;
(function (doc) {

  // 上传视频 链接
  var oOpenModBtn = doc.getElementsByClassName('js-open-mod-btn')[0],
    // 关闭模态框 X
    oCloseModBtn = doc.getElementsByClassName('js-close-mod-btn')[0],
    // 模态框 盒子
    oUploadMod = doc.getElementsByClassName('js-upload-modal')[0],
    //上传区域 按钮
    oUploadArea = doc.getElementsByClassName('js-upload-area')[0],
    //进度条
    oProgressBar = oUploadArea.getElementsByClassName('js-progress-bar')[0],
    //百分比
    oPercent = oUploadArea.getElementsByClassName('js-percent')[0],
    // 点击图片 触发上传按钮
    oVideoInput = doc.getElementById('videoFile'),
    //3Gb = 1024 * 1024 * 1024 * 3
    maxSize = 3221225472;

  var init = function () {
    bindEvent();
  }

  function bindEvent() {
    oOpenModBtn.addEventListener('click', showModal.bind(null, true), false);
    oCloseModBtn.addEventListener('click', showModal.bind(null, false), false);
    oVideoInput.addEventListener('change', uploadFile, false);



  }
  //是否显示模态框
  function showModal(show) {
    if (show) {
      oUploadMod.className += ' show';
    } else {
      oUploadMod.className = 'upload-modal-wrap js-upload-modal';
    }
  }

  function doUpload(file) {
    var fd = new FormData();
    fd.append('file', file);

    //请求数据
    var o = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
    o.open('post', 'server/upload.php')
    //新增uploading类名
    oUploadArea.className = 'upload-area js-upload-area uploading';
    //进度条
    o.upload.onprogress = function (e) {
      var e = e || window.event;
      //计算宽度
      oProgressBar.style.width = e.loaded / e.total * 100 + '%';
      //显示百分比
      oPercent.innerHTML = (e.loaded / e.total * 100).toFixed(1) + '%';
    }
    //加载完成事件
    o.upload.onload = function () {
      //新增finished类名
      oUploadArea.className = 'upload-area js-upload-area uploading finished';
      oPercent.innerHTML = '0.0%';
    }
    o.send(fd);
  }

  // 根据文本框变化触发上传文件
  function uploadFile() {
    //第一个视频
    var oFile = this.files[0],
      fileLen = oFile.length,
      fileName = oFile.name,
      fileSize = oFile.size;

    if (fileLen > 1) {
      console.log('一次只可上传一个视频');
      return;
    }

    if (!/\.(mp4)$/.test(fileName)) {
      console.log('您上传的' + fileName + '文件不是MP4视频格式');
      return;
    }

    if (fileSize > maxSize) {
      console.log('您上传的' + fileName + '文件超出可上传的文件大小');
      return;
    }

    //执行上传操作
    doUpload(oFile);

  }

  init();
})(document);